package com.multi.thread.chapter03.demo11;

import java.util.ArrayList;
import java.util.List;

public class MyStack {
	
	private List<String> list = new ArrayList<>();
	
	synchronized public void push() {
		try {
			while (list.size() == 1) {
				this.wait();
			}
			list.add("anyString = " + Math.random());
			this.notifyAll();
			System.out.println("push = " + list.size());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public String pop() {
		String returnString = "";
		try {
			while (list.size() == 0) {
				System.out.println("pop操作的：" + Thread.currentThread().getName() + "线程呈wait状态");
				this.wait();
			}
			returnString = "" + list.get(0);
			list.remove(0);
			this.notifyAll();
			System.out.println("pop = " + list.size());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return returnString;
	}

}
